/**
 * Created by admin on 2017/8/31.
 */
define(['app', 'filters/filters'], function(app) {
    app.register.controller('abilityAssess',['$scope','$state','$stateParams','$rootScope','$Ajax', function($scope, $state, $stateParams, $rootScope,$Ajax) {
        var http = app._baseUrl;
        $scope.active = false;
        $scope.nfocus = function(){
            $scope.active = true;
        };
        $scope.nfocus = {//搜索框状态
            active:false,
            focus:function(){
                this.active = true;
            },
            blur:function(){
                this.active = false;
            }
        };
        $scope.load = {
            _getInitial: function() { //专业类别和专业
                var url = http + '/QyManage/AbilityCheck/postAndLevelList';
                var data = {};
                $Ajax.postData(url,data,$scope).then(function(res){
                    if(res.data.success){
                        $scope.cate_level_list = res.data.data;
                    }
                })

            },
            _postStatisticsCount:function(){ //能力测评各岗位数据
                var url = http + '/QyManage/AbilityCheck/postStatisticsCount';
                var data = {};
                $Ajax.postData(url,data,$scope).then(function(res){
                    if(res.data.success){
                        $scope.countList = res.data.data;
                    }
                })

            },
            init: function() {
                this._getInitial && this._getInitial();
                this._postStatisticsCount && this._postStatisticsCount();
                // this._abilityList && this._abilityList(0,'');
            }
        };
        $scope.load.init && $scope.load.init();

    //    点击页面标签样式改变
        $scope.spanActive = {
            isGstyleActive:true,//岗位全部按钮
            isDstyleActive:true,//等级全部按钮
            cate_id:'0',
            check_level:'',
            g_ans:'',
            d_ans:'',
            real_name:'',
            isActive:function(type){//岗位、等级全部按钮发生点击事件
                this.activeAdd(type);
            },
            activeAdd:function(type){
                if(type==1){//岗位的全部按钮
                    this.isGstyleActive = !this.isGstyleActive;
                    $('.list_cate ul').find('span').removeClass('span_active');
                    if(this.isGstyleActive == false){
                        $('.list_cate ul li').eq(0).find('span').addClass('span_active');
                        this.g_ans = '1';
                        this.cate_id = '1';
                    }
                }
                if(type==2){//等级的全部按钮
                    this.isDstyleActive = !this.isDstyleActive;
                    $('.list_level ul').find('span').removeClass('span_active');
                    if(this.isDstyleActive == false){
                        $('.list_level ul li').eq(0).find('span').addClass('span_active');
                        this.d_ans = '0';
                        this.check_level = '0';
                    }
                }
                if(this.isDstyleActive){
                    this.check_level = '';
                    this.d_ans = '';

                }
                if(this.isGstyleActive){
                    this.cate_id = '0';
                    this.g_ans = '';
                }
                // $scope.pagedata.cate_ids = this.cate_id;
                // $scope.pagedata.check_levels = this.check_level;
                this.search();
            },
            isListActive:function(index,type,cate_id){
                if(type == 1){
                    if($('.list_cate ul li').eq(index).find('.span_active').length==1){
                        $('.list_cate ul li').eq(index).find('.span_active').removeClass('span_active');
                        if($('.list_cate ul').find('.span_active').length == 0){
                            this.isGstyleActive = true;
                            this.g_ans = '';
                        }else{
                            if(this.g_ans.indexOf(cate_id+',')==-1){//4,2,1---找4,
                                this.g_ans = this.g_ans.substring(0,this.g_ans.indexOf(cate_id)-1);
                            }else{
                                var a = this.g_ans.substring(0,this.g_ans.indexOf(cate_id));
                                var b = this.g_ans.substring(this.g_ans.indexOf(cate_id)+2);
                                this.g_ans = a + b;
                            }
                        }

                    }else{
                        if($('.list_cate ul').find('.span_active').length > 2){
                            $('.list_cate ul').find('span').removeClass('span_active');
                            $('.list_cate .span_all').addClass('span_active');
                            this.isGstyleActive = true;
                            this.g_ans = '';
                        }else{
                            this.isGstyleActive = false;
                            $('.list_cate .span_all').removeClass('span_active');
                            $('.list_cate ul li').eq(index).find('span').addClass('span_active');
                            if(this.g_ans==''){
                                this.g_ans += cate_id;
                            }else{
                                this.g_ans += ','+cate_id;
                            }
                        }
                    }
                }
                if(type == 2){
                    if($('.list_level ul li').eq(index).find('.span_active').length==1){
                        $('.list_level ul li').eq(index).find('.span_active').removeClass('span_active');
                        if($('.list_level ul').find('.span_active').length == 0){
                            this.isDstyleActive = true;
                            this.d_ans = '';
                        }else{
                            if(this.d_ans.indexOf(index+',')==-1){//4,2,1---找4,
                                this.d_ans = this.d_ans.substring(0,this.d_ans.indexOf(index)-1);
                            }else{
                                var a = this.d_ans.substring(0,this.d_ans.indexOf(index));
                                var b = this.d_ans.substring(this.d_ans.indexOf(index)+2);
                                this.d_ans = a + b;
                            }
                        }

                    }else{
                        if($('.list_level ul').find('.span_active').length > 2){
                            $('.list_level ul').find('span').removeClass('span_active');
                            $('.list_level .span_all').addClass('span_active');
                            this.isDstyleActive = true;
                            this.d_ans = '';
                        }else{
                            this.isDstyleActive = false;
                            $('.list_level .span_all').removeClass('span_active');
                            $('.list_level ul li').eq(index).find('span').addClass('span_active');
                            if(this.d_ans==''){
                                this.d_ans += index;
                            }else{
                                this.d_ans += ','+index;
                            }
                        }
                    }
                }
                this.cate_id = this.g_ans;
                this.check_level = this.d_ans;
                // $scope.pagedata.cate_ids = this.g_ans;
                // $scope.pagedata.check_levels = this.d_ans;
                this.search();
            },
            search:function(){
                $scope.pagedata.real_name = this.real_name;
                $scope.pagedata.cate_ids = this.cate_id;
                $scope.pagedata.check_levels = this.check_level;
                $scope.pagedata.change(null,1);
                // $scope.load._abilityList && $scope.load._abilityList(this.cate_id,this.check_level)
            },
            search_btn:function(){
                this.real_name = $scope.real_name;
                // $scope.pagedata.real_name = this.real_name;
                // $scope.pagedata.cate_ids = this.cate_id;
                // $scope.pagedata.check_levels = this.check_level;
                this.search();

            }
        };
    //    删除评测记录二次弹窗
        $scope.tipModule = {
            showTip:false,
            isAjax:true,
            id:'',
            show:function(id){
                this.isAjax = true;
                this.showTip = true;
                this.id = id;
            },
            hide:function(){
                this.showTip = false;
            },
            del:function(){
                this.show();
            },
            cancel:function(){
                this.hide();
            },
            confirm:function(){
                if(this.isAjax){
                    this.isAjax = false;
                    var url = http + '/QyManage/AbilityCheck/abilityDeleteForAdmin';
                    var data = {
                        ability_check_id:this.id
                    }
                    $Ajax.postData(url,data).then(function(res){
                        if(res.data.success){
                            $scope.load._postStatisticsCount();
                            $scope.spanActive.search();
                            $scope.tipModule.hide();
                        }
                    })
                }
            }
        };
    //    ------------------------------------------------获取token
        $scope.tokens = function(){
            var url = http + '/QyManage/AbilityCheck/createAbilityUrl';
            var data = {};
            $Ajax.postData(url,data).then(function(res){
               if(res.data.success){
                   $scope.token = res.data.data.token;
               }
            })
        };
        $scope.tokens();

        $scope.pagedata={
            url:app._baseUrl + "/QyManage/AbilityCheck/abilityList",
            pageSize:10,
            currentPage:1,
            returnValue:[],
            isShow:false,
            isKong:false,
            cate_ids:'',
            check_levels:'',
            real_name:'',
            loading_show:true,
            change:{}
        }


    //    ----------------------------------------------------------------------绑定搜索
        document.onkeydown = function (e) {
            var ev = document.all ? window.event : e;
            if (ev.keyCode == 13) {
                $scope.spanActive.real_name = $scope.real_name;
                $scope.spanActive.search();
            }
        };


    }])
})